<?php

	class EventDataAccessMySQL implements iEventDataAccess {
		
		public function buildObject($result) {
			
			$event = new Event();
			
			$event->setEventId($result->eventId);
			$event->setName($result->name);
			$event->setPicture($result->picture);
			$event->setStartDate(formatDate($result->startDate));
			$event->setEndDate(formatDate($result->endDate));
			$event->setDescription($result->description);
			$event->setType($result->type);
			
			$eventUser = new EventUser();
			
			$user = new User();
			$user->setUserId($result->userId);
			$user->setName($result->userName);
			
			$invitationUser = new User();
			$invitationUser->setUserId($result->invitationUserId);
			$invitationUser->setName($result->invitationUserName);
			
			$eventUser->setInvitationUser($invitationUser);
			$eventUser->setUser($user);
			$eventUser->setIsUserManager($result->isManager);
			$eventUser->setIsUserConfirmed($result->isConfirmed);
			$eventUser->setUserRegistrationCode($result->registrationCode);
						
			$event->setEventUser($eventUser);
						
			return $event;
		}
		
		public function select($event, $dataBaseConnection) {
			
			$resultArray = array();
			
			if ($event->getEventUser()->getUser()->getUserId() != null){
				$parameters["USERID"] = $event->getEventUser()->getUser()->getUserId();
			} else {
				$parameters["USERID"] = "NULL";
			}
			
			if ($event->getEventId() != null){
				$parameters["EVENTID"] = $event->getEventId();
			}else {
				$parameters["EVENTID"] = "NULL";
			}
			if ($event->getName() != null){
				$parameters["NAME"] = "'" . $event->getName() . "'";
			}else {
				$parameters["NAME"] = "NULL";
			}
			if ($event->getDescription() != null){
				$parameters["DESCRIPTION"] = "'" . $event->getDescription() . "'";
			}else {
				$parameters["DESCRIPTION"] = "NULL";
			}			
			if ($event->getStartDate() != null){
				$parameters["STARTDATE"] = "'" . formatDateMySQL($event->getStartDate()) . "'";
			}else {
				$parameters["STARTDATE"] = "NULL";
			}
			if ($event->getEndDate() != null){
				$parameters["ENDDATE"] = "'" . formatDateMySQL($event->getEndDate()) . "'";
			}else {
				$parameters["ENDDATE"] = "NULL";
			}
			if ($event->getType() != null){
				$parameters["TYPE"] = "'" . $event->getType() . "'";
			}else {
				$parameters["TYPE"] = "NULL";
			}
			if ($event->getPicture() != null){ //TODO pensar como vai inserir a foto
				$parameters["PICTURE"] = "'" . $event->getPicture() . "'";
			}else {
				$parameters["PICTURE"] = "NULL";
			}
			
			$query = buildQuery("querySelectEvent", $parameters);

			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}
		
	}

?>